#include <StdDef.h>
#include <StdLib.h>
#include <StdIO.h>

#include <Resources.h>
#include	<ToolUtils.h>

#define	begin		{
#define	end		}


typedef struct {
	short int	LowBounds;
	short int	ResNum;
} errRef;

typedef struct {
	short int	ItemCount;
	errRef		Items[1];
} elut, *elutPtr, **elutHdl;


/*----------------------------------------*/
/* reportFailure									*/
/* Tell user of error in OSerr and abort.	*/
/*----------------------------------------*/
void reportFailure(char *message, char *miscInfo)
	begin
		fprintf(stderr,"### OSErr: %s\n",message,miscInfo);
		exit(1);
	end


/*----------------------------------------*/
/* doMsg										*/
/* Write MacOS meaning to StdOut				*/ 
/*----------------------------------------*/
void doMsg(int errNum, int resNum, int indexNum)
	begin
		char	message[256];
		
		GetIndString(message,resNum,indexNum);
		printf("OSERR %d: %P\n", errNum, (message[0]) ? message : "\pNo such message");
	end /* doMsg */
	

/*----------------------------------------*/
/* main												*/
/*----------------------------------------*/
int main(int argc, char **argv)
	begin
		elutHdl	theTable;
		int		i;
		int		j;
		int		errNum;
		int		bounds;
		
		//
		//--- See if the user typed any arguments
		//
		if (argc<2)
			reportFailure("UsageÉ OSERR [error number]", NULL);
		
		//
		//--- Read the table that will tell us what STR# resource the message is in
		//
		theTable	= (elutHdl )GetResource( (ResType)'eLUt',127);
		if (!theTable)
			reportFailure("Can't load 'eLUt' resource.", NULL);
	
	
		//
		//--- Main loop
		//
		for(i=1; i<argc; i++)
			begin
				errNum	= atoi(argv[i]);
				if (errNum >= 0)
					reportFailure("Error number must be negative. (%s)", argv[i]);
				
				errNum	= (-errNum);
					
				for(j=0; j<(**theTable).ItemCount; j++)
					begin
						bounds	= (**theTable).Items[j].LowBounds;
						if ((errNum >= bounds) && (errNum <= (bounds+100)))
							doMsg(errNum,(**theTable).Items[j].ResNum,errNum-bounds+1); 
					end /* for */
					
			end /* for */
		
		return 0;

	end /* main */

